package com.youkeda.app.api;

import com.youkeda.app.model.Category;
import com.youkeda.app.model.Result;
import com.youkeda.app.service.CategoryService;
import com.youkeda.app.service.SnappedUpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName CategoryAPI
 * @Author 刘正星
 * @Date 2020/8/20 17:19
 **/
@RestController
public class CategoryApi {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private SnappedUpService snappedUpService;

    @Autowired
    private CategoryService categoryService;
    @PostMapping("/category/add")
    public Result<Category> add(@RequestBody Category category){
        Result result = new Result();
        Category insertCategory = categoryService.add(category);
        if (insertCategory == null){
            result.setMessage("插入失败");
            result.setCode("100001");
        }
        result.setSuccess(true);
        result.setData(insertCategory);
        return result;
    }

    @GetMapping("/category/getAll")
    public Result<List<Category>> queryAll(){
        Result result = new Result();
        List<Category> categoryList = categoryService.queryAll();
        if (categoryList == null){
            result.setSuccess(false);
            result.setMessage("false");
        }
        result.setSuccess(true);
        result.setData(categoryList);
        return result;
    }

    @GetMapping("/category/get")
    public Result<Category> get(@RequestParam("id")String id){
        Result result = new Result();
        Category category = categoryService.get(id);
        if (category == null){
            result.setSuccess(false);
            return result;
        }
        result.setSuccess(true);
        result.setData(category);
        return result;
    }

    /**
     * redis 插入
     * @param category
     * @return
     */

    @PostMapping("/create")
    public Result<Category> insert(@RequestBody Category category){
        Result result = new Result();
        result.setSuccess(true);
        redisTemplate.opsForList().leftPush("categoryList",category);
        result.setData(category);
        return result;

    }

    @GetMapping("/queryAll")
    public Result<List<Category>> getAll(){
        Result result = new Result();
        result.setSuccess(true);
        //取出所有类目
        List<Category> categoryList = redisTemplate.opsForList().range("categoryList",0,-1);
       //如果parentId 为空则是父类目
        List<Category> parentList = categoryList.stream().filter(category -> {
            return StringUtils.isEmpty(category.getParentCategoryId());
        }).collect(Collectors.toList());
        //处理父子类目关系
        categoryList.forEach(category -> {
            parentList.forEach(parentCategory->{
                if (category.getParentCategoryId().equals(parentCategory.getId())){
                    if (CollectionUtils.isEmpty(parentCategory.getSubCategoryList())){
                        List<Category> subCategoryList = new ArrayList<>();
                        subCategoryList.add(category);
                        parentCategory.setSubCategoryList(subCategoryList);
                    }
                    parentCategory.getSubCategoryList().add(category);
                }
            });
        });
        result.setData(parentList);
        return result;
    }

    @GetMapping("/get")
    public Result getById(@RequestParam("id")String id){
        Result result = new Result();
        List<Category> categoryList = redisTemplate.opsForList().range("categoryList",0,-1);
        if (StringUtils.isEmpty(id)){
            result.setSuccess(false);
            return result;
        }
          Category category = null;
        for (Category item:categoryList
             ) {
            if (item.getId().equals(id)){
               category = item;
            }
        }
        result.setSuccess(true);
        result.setData(category);
        return result;
    }
    @GetMapping("/snappedUp")
    public Result snappedUp(@RequestParam("id")Long id){
        return snappedUpService.snappedUp(id);
    }

}
